using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Components.Security;
using Framework.Library.Components.Commands;
using Framework.Library;
using Framework.Library.Components.Database;
using Framework.Library.Utilities;
using System.Collections.Generic;

namespace Framework.Portal.Administrator.Database
{
    public partial class Tables : AuthenticatedControl
    {
        _Table objTable = new _Table();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!Page.IsPostBack)
            {
                lblCommandName.Text = AppEnv.CommandName();
                LoadData();
            }
        }

        private void LoadData()
        {
            dtgInfos.DataSource = objTable.GetTables();
            dtgInfos.ItemDataBound +=new RepeaterItemEventHandler(dtgInfos_ItemDataBound);
            dtgInfos.DataBind();
        }
        
        protected void dtgInfos_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Item.DataItem;
            HtmlInputCheckBox myCheckDrop = (HtmlInputCheckBox)e.Item.FindControl("checkDrop");
            if (myCheckDrop != null)
            {
                myCheckDrop.Value = rowView["name"].ToString();
            }
        }

        protected void dtgInfos_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Property":
                    Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.TableProperty)) + "&id=" + id);
                    break;
                case "Drop":
                    Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.TableAction)) + "&id=" + id + "&action=drop");
                    break;
                case "Rename":
                    Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.TableAction)) + "&id=" + id + "&action=rename");
                    break;
                default:
                    break;                
            }
            LoadData();
        }

        protected void btnNewTable_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.TableNew)));
        }

        protected void btnDropTable_Click(object sender, EventArgs e)
        {
            List<RepeaterItem> removed = new List<RepeaterItem>();
            foreach (RepeaterItem items in dtgInfos.Items)
            {
                removed.Add(items);
            }
            foreach (RepeaterItem it in removed)
            {
                HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)it.FindControl("checkDrop");
                if (deleteChkBxItem.Checked)
                {
                    string strTemp = deleteChkBxItem.Value;
                    try
                    {
                        objTable.DropTable(strTemp);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
            LoadData();
        }
    }
}